package com.maben.repository;

import com.maben.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface UserRepository extends JpaRepository<User, Long> {

    /**
     * 根据一个字段，精准匹配
     * 返回集合
     */
    List<User> findByUsername(String username)throws Exception;

    /**
     * 等价于 SQL: WHERE username LIKE %?%
     */
    List<User> findByUsernameContaining(String username);

    /**
     * 等价于 SQL: WHERE username LIKE ?%
     */
    List<User> findByUsernameStartingWith(String username);

    /**
     * 等价于 SQL: WHERE username LIKE %?% AND phone = ?
     */
    List<User> findByUsernameContainingAndPhone(String username, String phone);

    /**
     * 忽略大小写的模糊搜索
     */
    List<User> findByUsernameContainingIgnoreCase(String username);

    /**
     * 使用SQL查询
     */
    @Query(
            nativeQuery = true,
            value = "select * from sys_user where username like %:username%"
    )
    List<User> findByUsernameVague(@Param("username") String username)throws Exception;

}
